package com.mycom.controller;

import com.mycom.controller.constant.Code;
import com.mycom.controller.entity.Result;
import com.mycom.pojo.*;
import com.mycom.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/teamWaitReviews")
public class TeamWaitReviewController {
    @Autowired
    private TeamSubmitFormService teamSubmitFormService;
    @Autowired
    private TeamSubmitItemService teamSubmitItemService;
    @Autowired
    private TeamSubmitWaitCertainService teamSubmitWaitCertainService;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private RefuseConfirmReasonService refuseConfirmReasonService;

//    @PostMapping
//    public Result save(@RequestBody TeamSubmitForm teamSubmitForm){
//        Employee employee = employeeService.findByIdCard(teamSubmitForm.getIdCardNumber());
//        String formNumber = employee.getDepartmentId() + "-" + employee.getId() + "-" + teamSubmitForm.getSubmitDate().getTime();
//        teamSubmitForm.setFormNumber(formNumber);
//        teamSubmitForm.setTeamState(0);
//        //临时指定审核人，后期完成权限管理再修改
//        teamSubmitForm.setReviewer("林生");
//        boolean flag1 = teamSubmitFormService.save(teamSubmitForm);
//        Integer formId = teamSubmitForm.getId();
//        List<TeamSubmitItem> teamSubmitItems = teamSubmitForm.getSubmitItems();
//        boolean flag2 = true;
//        for (TeamSubmitItem teamSubmitItem : teamSubmitItems) {
//            teamSubmitItem.setFormId(formId);
//            boolean flag3 = teamSubmitItemService.save(teamSubmitItem);
//            flag2 = flag2 & flag3;
//        }
//        List<TeamSubmitWaitCertain> members = teamSubmitForm.getMembers();
//        for (TeamSubmitWaitCertain member : members) {
//            //填报人不用确认报表
//            if (member.getUsername().equals(teamSubmitForm.getUsername())){
//                member.setState(1);
//            }else{
//                member.setState(0);
//            }
//            member.setFormId(formId);
//            boolean flag3 = teamSubmitWaitCertainService.save(member);
//            flag2 = flag2 & flag3;
//        }
//        boolean flag = flag1 & flag2;
//        return new Result(flag ? Code.SAVE_SUCCESS : Code.SAVE_ERR,flag);
//    }

    /**
     * 查询待审核报表
     * @param idCardNumber
     * @return
     */
    @GetMapping("/{idCardNumber}")
    public Result findByIdCard(@PathVariable String idCardNumber){
        List<TeamSubmitForm> teamSubmitForms = new ArrayList<>();
        List<TeamSubmitWaitCertain> teamSubmitWaitCertainList = teamSubmitWaitCertainService.findByIdCardAndState(idCardNumber, 1);
        for (TeamSubmitWaitCertain teamSubmitWaitCertain : teamSubmitWaitCertainList) {
            Integer formId = teamSubmitWaitCertain.getFormId();
            TeamSubmitForm teamSubmitForm = teamSubmitFormService.findById(formId);
            List<TeamSubmitItem> teamSubmitItems = teamSubmitItemService.findByFormId(formId);
            teamSubmitForm.setSubmitItems(teamSubmitItems);
            List<TeamSubmitWaitCertain> members = teamSubmitWaitCertainService.findByFormId(formId);
            teamSubmitForm.setMembers(members);
            teamSubmitForms.add(teamSubmitForm);
        }
        Integer code = teamSubmitForms!=null ? Code.QUERY_ALL_SUCCESS : Code.QUERY_ALL_ERR;
        String msg = teamSubmitForms!=null ? "查询待审核报表成功！" : "查询待审核报表失败！";
        return new Result(code,teamSubmitForms,msg);
    }

//    /**
//     * 确认
//     * @param member
//     * @return
//     */
//    @RequestMapping("/confirmCertain")
//    public Result confirmCertain(@RequestBody TeamSubmitWaitCertain member){
//        member.setState(1);
//        boolean flag = teamSubmitWaitCertainService.updateState(member);
//        Integer code = flag ? Code.SAVE_SUCCESS : Code.SAVE_ERR;
//        String msg = flag ? "确认个人报表成功！" : "确认个人提交报表失败！";
//        return new Result(code,flag,msg);
//    }
//
//    /**
//     * 拒绝确认
//     * @param refuseConfirmReason
//     * @return
//     */
//    @RequestMapping("/refuseCertain")
//    public Result refuseCertain(@RequestBody RefuseConfirmReason refuseConfirmReason){
//        //保存拒绝确认的原因
//        boolean flag1 = refuseConfirmReasonService.save(refuseConfirmReason);
//        Integer memberId = refuseConfirmReason.getMemberId();
//        TeamSubmitWaitCertain member = teamSubmitWaitCertainService.findById(memberId);
//        member.setState(-1);
//        boolean flag2 = teamSubmitWaitCertainService.updateState(member);
//        boolean flag = flag1 & flag2;
//        Integer code = flag ? Code.SAVE_SUCCESS : Code.SAVE_ERR;
//        String msg = flag ? "拒绝确认个人报表成功！" : "拒绝确认个人提交报表失败！";
//        return new Result(code,flag,msg);
//    }
}
